<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="${ctx}/res/css/default.css">
<link rel="stylesheet" type="text/css" href="${ctx}/res/css/easyui.css" />
<link rel="stylesheet" type="text/css" href="${ctx}/res/css/icon.css" />
<link rel="stylesheet" type="text/css" href="${ctx}/res/css/style.css">
<link rel="stylesheet" type="text/css"
	href="${ctx}/res/css/lq.fieldset.css">
<script type="text/javascript" src="${ctx}/res/js/jquery.min.js"></script>
<script type="text/javascript" src="${ctx}/res/js/jquery.easyui.min.js"></script>
<script type="text/javascript" src="${ctx}/res/js/easyui-lang-zh_CN.js"></script>
<script type="text/javascript" src="${ctx}/res/js/lq.fieldset.js"></script>
<script type="text/javascript" src="${ctx}/res/js/base.js"></script>
<script type="text/javascript" src="${ctx}/res/js/pager.js"></script>
<script type="text/javascript">
	$(function() {
		doSearch();
	})
	//查询数据
	function paramData() {
		var param = {}
		return param;
	}
	//查询
	function doSearch() {
		var param = paramData();
		$("#list").datagrid({
			url : "${ctx}/administrator/search",
			queryParams : param,
			loadFilter : pagerFilter
		});
	}
	//去添加
	function toAdd(appen) {
		$("#fs").form("clear");
		$("#adminMethod").val(appen);
		$('#aPass').textbox("enable");
		$('#repassword').textbox("enable");
		if (appen == 2) {
			toUpdateUnit();
		}
		$('#AdminAdd').window('open');
	}
	//修改
	function toUpdateUnit() {
		var row = $("#list").datagrid("getSelected");
		$('#aId').val(row.aId);
		$("#aName").textbox("setValue", row.aName);
		$("#aPass").textbox("setValue", row.aPass);
		$('#aPass').textbox("disable");
		$("#repassword").textbox("setValue", row.aPass);
		$('#repassword').textbox("disable");

		$("#aHeader").textbox("setValue", row.aHeader);
		$("#aPhone").textbox("setValue", row.aPhone);
		$("#aEmail").textbox("setValue", row.aEmail);
	}

	//保存
	function save() {

		var adminMethod = $("#adminMethod").val();
		if (adminMethod == 1) {
			var url = "${ctx}/administrator/add";
		}
		if (adminMethod == 2) {
			var url = "${ctx}/administrator/update";
		}
		var aId = $("#aId").val();
		var aName = $("#aName").textbox("getValue");
		var aPass = $("#aPass").textbox("getValue");
		var repassword = $("#repassword").textbox("getValue");
		var aPhone = $("#aPhone").textbox("getValue");
		var aEmail = $("#aEmail").textbox("getValue");
		var aHeader = $("#aHeader").textbox("getValue");

		if (aPass != repassword) {
			window.alert("两次密码不一致");
			return false;
		}

		if (aName == null || aName == '') {
			window.alert("用户名不能为空");
			return false;
		}
		if (aPass == null || aPass == '') {
			window.alert("密码不能为空");
			return false;
		}

		var postData = {
			'aId' : aId,
			'aName' : aName,
			'aPass' : aPass,
			'aHeader' : aHeader,
			'aPhone' : aPhone,
			'aEmail' : aEmail
		};
		$.ajax({
			url : url,
			data : postData,
			type : 'POST',
			success : function(result) {
				if (result) {
					$info('保存成功');
					doSearch();
				}
			}
		});
	}
	//删除
	function toDelete() {
		var row = $("#list").datagrid("getSelected");//获取选中行的值
		var data = {
			'aId' : row.aId
		}
		$.ajax({
			type : "post",
			url : "${ctx}/administrator/del",
			data : data,
			success : function(msg) {
				if (msg) {
					$info("删除成功!");
					doSearch();
				}
			}
		})
	}
	function imgFormatter(value, row, index) {
    	if('' != value && null != value) {
    		value = '../res/' + value;
      		value = '<img style="width:50px; height:40px" src="' + value + '">';
    	}
      	return  value;
    }
</script>
<div id="tb"
	style="padding: 5px; height: auto; background-color: #daeef5; text-align: right;">
	<a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true"
		onclick="toAdd(1)">添加</a>
</div>

<div class="easyui-layout" fit="true">
	<div region="east" style="width: 100%; height: 100%" id="eastTable">
		<table class="easyui-datagrid" style="width: 100%; height: 100%;"
			data-options="rownumbers:true,singleSelect:true,autoRowHeight:false,pagination:true,
				pageSize:10,toolbar:'#tb',onRowContextMenu:function(e, rowIndex, rowData){
					e.preventDefault();
					$(this).datagrid('selectRow',rowIndex);
					$('#menu-northTable').menu('show',{
						left: e.pageX,
						top: e.pageY
					});
				}"
			id="list">
			<input type="hidden"
				data-options="field:'aId',width:'10%',halign:'center'" />
			<input type="hidden" id="adminMethod" name="adminMethod" />
			<input data-options="field:'aPass',width:'10%',halign:'center'"
				type="hidden" />
			<thead>
				<tr>
					<th data-options="field:'aName',width:'10%',halign:'center'">用户名</th>
					<th
						data-options="field:'aPhone',width:'10%',halign:'center',align:'center'">联系电话</th>
					<th
						data-options="field:'aEmail',width:'10%',halign:'center',align:'center'">email</th>
					<th
						data-options="field:'aHeader',width:'20%',halign:'center',align:'center',formatter:imgFormatter">图片</th>
				</tr>
			</thead>
		</table>
	</div>
</div>
<!--  管理员管理	-->
<div id="AdminAdd" class="easyui-dialog" title="管理员管理"
	collapsible="false"
	style="width: 550px; height: 250px; padding: 0 5px; background: #fafafa;"
	data-options="closed:true,modal:true,minimizable: false,maximizable: false,
				iconCls: 'icon-save',
				buttons: [{
					text:'保存',
					iconCls:'icon-ok',
					handler:function(){
						save();
						$('#AdminAdd').dialog('close');
					}
				},{
					text:'取消',
					iconCls:'icon-cancel',
					handler:function(){
					$('#AdminAdd').dialog('close');
					}
				}]
			">
	<table width="100%">
		<tr height="50px">
			<td width="20%" style="text-align: right">用户名：</td>
			<td width="30%"><input id="aName" class="easyui-textbox"
				required="true" style="width: 90%; height: 22px" /><font
				color="red">*</font></td>
			<td width="20%" style="text-align: right">图片：</td>
			<td width="30%"><input id="aHeader" class="easyui-textbox"
				required="true" style="width: 90%; height: 22px" /><font
				color="red">*</font></td>
		</tr>
		<tr height="50px">
			<td width="20%" style="text-align: right" />密码：
			</td>
			<td width="30%"><input id="aPass" name="aPass"
				class="easyui-textbox" required="true" type="password" value=""
				style="width: 90%; height: 22px" /><font color="red">*</font></td>
			<td style="text-align: right">确认密码：</td>
			<td><input type="password" name="repassword" id="repassword"
				required="true" class="easyui-textbox" /></td>
		</tr>
		<tr height="50px">
			<td width="20%" style="text-align: right">电话：</td>
			<td width="30%"><input id="aPhone" class="easyui-numberbox"
				style="width: 90%; height: 22px" /></td>
			<td width="20%" style="text-align: right">EMAIL：</td>
			<td width="30%"><input id="aEmail" class="easyui-textbox"
				data-options="prompt:'Enter a valid email.',validType:'email'"><input
				id="aId" type="hidden" /></td>
		</tr>
	</table>
</div>
</div>
<!-- 数据列表上下文 -->
<div id="menu-northTable" class="easyui-menu" style="width: 120px;">
	<div onclick="toAdd(2)" data-options="iconCls:'icon-edit'">修改用户信息</div>
	<div class="menu-sep"></div>
	<div onclick="toDelete()" data-options="iconCls:'icon-remove'">删除用户信息</div>
</div>
<!-- 数据列表上下文 end -->
</body>
c
</html>